import axios from 'axios';
import Vue from 'vue';
import router from '../router';
import { Message } from 'element-ui';
axios.defaults.baseURL = "http://localhost:3000";
Vue.prototype.axios=axios
//请求拦截器
axios.interceptors.request.use(
  (config) => {
    // Do something before request is sent
    const token = localStorage.getItem("token");
    if (token) config.headers.Authorization = `Bearer ${token}`;
    return config;
  },
  (error) => {
    // Do something with request error
    return Promise.reject(error);
  }
);

//响应拦截器
axios.interceptors.response.use(
  (response) => {
    // Do something before response is sent
    console.log(response);
    const status = response.data.code;
    const msg = response.data.msg;
    // Toast.fail(msg);
    if (status === 402) 
    {
      Message({
        showClose: true,
        message: '未合法登录',
        type: 'warning'
      });
      router.push('/login')
    }
    return response;
  },
  (error) => {
    // Do something with response error
    return Promise.reject(error);
  }
);